﻿using System.Collections.Generic;
using CNF.Common.Base.SimpleOrm.Data;
using CNF.Common.Base.SimpleOrm.DataBaseProvidrer;
using CNF.Common.Base.SimpleOrm.Domain;
using CNF.Common.Base.SimpleOrm.Entities;
using PetaPoco;

namespace CNF.Common.Base.SimpleOrm.ServiceProxyies
{
    public abstract class BaseServiceProxy<DataKeyType, EntityType, DataServiceType, DomainType> : BaseViewServiceProxy<DataKeyType, EntityType, DataServiceType, DomainType>, IServiceProxy<DataKeyType, EntityType, DataServiceType, DomainType>
        where DataKeyType : struct
        where EntityType : BaseEnity<DataKeyType>, new()
        where DataServiceType : BaseDataService<DataKeyType, EntityType>
        where DomainType : BaseDomain, new()
    {



        protected BaseServiceProxy(IDataBaseStoreage<Database> dataBaseStoreage, string dataBaseKey, IServiceContainer serviceContainer)
            : base(dataBaseStoreage, dataBaseKey, serviceContainer)
        {
        }
 
 
        public void Insert(DomainType domain)
        {
            EntityType entity = ConvertToDbEntity(domain);
            this.CurrentDataService.Insert(entity);
            domain.DataKey = entity.GetDataKey();
        }

        protected abstract EntityType ConvertToDbEntity(DomainType domain);

        public void Update(DomainType domain)
        {
            this.CurrentDataService.Update(ConvertToDbEntity(domain));
        }
 
        public void Save(DomainType domain)
        {
            EntityType entity = ConvertToDbEntity(domain);
            this.CurrentDataService.Save(entity);
            domain.DataKey = entity.GetDataKey();
        }

        public void DeleteById(DataKeyType dataKey)
        {
            this.CurrentDataService.DeleteById(dataKey);
        }

        public void Delete(DomainType domain)
        {
            this.CurrentDataService.Delete(ConvertToDbEntity(domain));
        }

 
    }
}
